local anxu = fk.CreateSkill {

  name = "joy__anxu",

  tags = {  },

}



anxu:addEffect(fk.EventPhaseStart, {
  name = "joy__anxu",
  anim_type = "control",
  events = {fk.EventPhaseStart,fk.EventPhaseEnd},
  can_trigger = function(self, event, target, player, data)
    local room = player.room
    local targets = {}
    if target == player and player:hasSkill(anxu.name) then
      local max = 0
      for _, p in ipairs(room:getOtherPlayers(player)) do
        if p:getHandcardNum() > max then
          max = p:getHandcardNum()
          targets = {}
        end
        if p:getHandcardNum() == max then
          table.insert(targets, p.id)
        end
      end
      return player.phase == Player.Play  and #targets > 0 and max > 0
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local targets = {}
    local max = 0
      for _, p in ipairs(room:getOtherPlayers(player)) do
        if p:getHandcardNum() > max then
          max = p:getHandcardNum()
          targets = {}
        end
        if p:getHandcardNum() == max then
          table.insert(targets, p.id)
        end
      end
    if #targets > 0 then
      local target = room:askForChoosePlayers(player,targets,1,1,"#joy__anxu-invoke",anxu.name,true)
      if #target > 0 then
        event:setCostData(self, target[1])
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(event:getCostData(self))
    local card = room:askForCardChosen(player,to,"h",anxu.name,"#joy__anxu-choose::"..to.id)
    if card > 0 then
      room:obtainCard(player,card,false,fk.ReasonPrey)
    end
    if Fk:getCardById(card).suit == Card.Spade and not to.dead then
      to:drawCards(1,anxu.name)
    end
  end,
})
anxu:addEffect(fk.EventPhaseEnd, {
  name = "joy__anxu",
  anim_type = "control",
  events = {fk.EventPhaseStart,fk.EventPhaseEnd},
  can_trigger = function(self, event, target, player, data)
    local room = player.room
    local targets = {}
    if target == player and player:hasSkill(anxu.name) then
      local max = 0
      for _, p in ipairs(room:getOtherPlayers(player)) do
        if p:getHandcardNum() > max then
          max = p:getHandcardNum()
          targets = {}
        end
        if p:getHandcardNum() == max then
          table.insert(targets, p.id)
        end
      end
      return player.phase == Player.Play  and #targets > 0 and max > 0
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local targets = {}
    local max = 0
      for _, p in ipairs(room:getOtherPlayers(player)) do
        if p:getHandcardNum() > max then
          max = p:getHandcardNum()
          targets = {}
        end
        if p:getHandcardNum() == max then
          table.insert(targets, p.id)
        end
      end
    if #targets > 0 then
      local target = room:askForChoosePlayers(player,targets,1,1,"#joy__anxu-invoke",anxu.name,true)
      if #target > 0 then
        event:setCostData(self, target[1])
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(event:getCostData(self))
    local card = room:askForCardChosen(player,to,"h",anxu.name,"#joy__anxu-choose::"..to.id)
    if card > 0 then
      room:obtainCard(player,card,false,fk.ReasonPrey)
    end
    if Fk:getCardById(card).suit == Card.Spade and not to.dead then
      to:drawCards(1,anxu.name)
    end
  end,
})

return anxu